package com.ayf.payment.game.api.service.platform.impl;

import com.ayf.payment.game.api.cache.redis.RedisCacheUtil;
import com.ayf.payment.game.api.dto.WxConfigDTO;
import com.ayf.payment.game.api.entity.MerchantConfig;
import com.ayf.payment.game.api.entity.MerchantLevelRelation;
import com.ayf.payment.game.api.entity.platform.WxConfig;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.mapper.platform.WxConfigMapper;
import com.ayf.payment.game.api.service.MerchantConfigService;
import com.ayf.payment.game.api.service.MerchantLevelRelationService;
import com.ayf.payment.game.api.service.platform.WxConfigService;
import com.ayf.payment.game.api.type.WxConstant;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class WxConfigServiceImpl implements WxConfigService {

    @Resource
    private WxConfigMapper wxConfigMapper;
    @Resource
    private RedisCacheUtil redisCacheUtil;

    @Autowired
    private MerchantLevelRelationService merchantLevelRelationService;
    @Autowired
    private MerchantConfigService merchantConfigService;

    @Override
    public WxConfig getConfig() {
        WxConfig wxConfig = redisCacheUtil.getObjectFromCache("GAME_PAY_WX_CONFIG:PLATFORM_WX");
        if (wxConfig == null) {
            WxConfig wxConfig1 = wxConfigMapper.selectByPrimaryKey(1);
            if (wxConfig1 == null) {
                WxConfig dbConfig = new WxConfig();
                dbConfig.setId(1);
                dbConfig.setStatus(0);
                wxConfigMapper.insertSelective(dbConfig);
                return dbConfig;
            } else {
                redisCacheUtil.setObjectToCache("GAME_PAY_WX_CONFIG:PLATFORM_WX", wxConfig1, 7200L, TimeUnit.SECONDS);
                return wxConfig1;
            }
        } else {
            return wxConfig;
        }
    }

    @Override
    public WxConfig getGameConfig(Integer id) {
        WxConfig wxConfig = redisCacheUtil.getObjectFromCache("GAME_PAY_WX_CONFIG:MERCHANT_WX_" + id);
        if (wxConfig == null) {
            WxConfig wxConfig1 = wxConfigMapper.selectByPrimaryKey(id);
            if (wxConfig1 == null) {
                WxConfig dbConfig = new WxConfig();
                dbConfig.setId(id);
                dbConfig.setStatus(0);
                dbConfig.setMenuConfig(WxConstant.DEFAULT_WECHAT_MENU_CONFIG);
                wxConfigMapper.insertSelective(dbConfig);
                return dbConfig;
            } else {
                redisCacheUtil.setObjectToCache("GAME_PAY_WX_CONFIG:MERCHANT_WX_" + id, wxConfig1, 7200L, TimeUnit.SECONDS);
                return wxConfig1;
            }
        } else {
            return wxConfig;
        }
    }


    @Override
    public WxConfig getGameConfig(String wxOriginId) {
        WxConfig wxConfig = redisCacheUtil.getObjectFromCache("GAME_PAY_WX_CONFIG:MERCHANT_WX_" + wxOriginId);
        if (wxConfig == null) {
            WxConfig tempWxConfig = new WxConfig();
            tempWxConfig.setWxOriginId(wxOriginId);
            WxConfig wxConfig1 = wxConfigMapper.selectOne(tempWxConfig);
            if (wxConfig1!=null && StringUtils.isEmpty(wxConfig1.getMenuConfig())){
                wxConfig1.setMenuConfig(WxConstant.DEFAULT_WECHAT_MENU_CONFIG);
            }
            redisCacheUtil.setObjectToCache("GAME_PAY_WX_CONFIG:MERCHANT_WX_" + wxOriginId, wxConfig1, 7200L, TimeUnit.SECONDS);
            return wxConfig1;
        } else {
            return wxConfig;
        }
    }

    @Override
    public WxConfig getGameConfigByMerchantRelationId(Integer merchantId) {
        MerchantConfig merchantConfig = merchantConfigService.selectOne(merchantId);
        if (merchantConfig.getWechatMbType() != null && merchantConfig.getWechatMbType()==1){
            return getGameConfig(2); // 查询平台动态密保公众号配置信息
        } else {
            MerchantLevelRelation merchantLevelRelation=merchantLevelRelationService.selectOne(merchantId);
            if (merchantLevelRelation == null){
                return BeanConvertUtils.convert(selectByMId(merchantId),WxConfig.class);
            } else {
                if(merchantLevelRelation.getSuperMerchantId()>0){
                    return BeanConvertUtils.convert(selectByMId(merchantLevelRelation.getSuperMerchantId()),WxConfig.class);
                }else{
                    return BeanConvertUtils.convert(selectByMId(merchantLevelRelation.getMerchantId()),WxConfig.class);
                }
            }
        }
    }

    @Override
    public boolean updateConfig(WxConfig config) {
        if (config.getId() == null) {
            throw new ParameterException("id不能为空");
        }
        if (config.getId() == 1) {
            redisCacheUtil.delete("GAME_PAY_WX_CONFIG:PLATFORM_WX");
            redisCacheUtil.delete("GAME_PAY_WX_CONFIG:PLATFORM_WX_ACCESS_TOKEN");
        } else if (config.getId() == 2) {
            config.setStatus(1);
            redisCacheUtil.delete("GAME_PAY_WX_CONFIG:MERCHANT_WX_2");
            redisCacheUtil.delete("GAME_WX_API_TOKEN_" + config.getWxOriginId());
        } else {
            redisCacheUtil.delete("GAME_PAY_WX_CONFIG:MERCHANT_WX_" + config.getWxOriginId());
            redisCacheUtil.delete("GAME_WX_API_TOKEN_" + config.getWxOriginId());
        }
        return wxConfigMapper.updateByPrimaryKeySelective(config) > 0;
    }

    @Override
    public void insert(WxConfig config) {
        wxConfigMapper.insert(config);
    }

    @Override
    public WxConfigDTO selectByMId(int mId) {
        WxConfig config=new WxConfig();
        config.setMerchantId(mId);
        WxConfig wxConfig=wxConfigMapper.selectOne(config);
        if(null==wxConfig){
            wxConfig=new  WxConfig();
            wxConfig.setStatus(0);
        }
        WxConfigDTO wxConfigDTO = BeanConvertUtils.convert(wxConfig,WxConfigDTO.class);
        MerchantConfig merchantConfig = merchantConfigService.selectOne(mId);
        if (merchantConfig==null){
            wxConfigDTO.setWechatMbType(0);
        } else {
            wxConfigDTO.setWechatMbType(merchantConfig.getWechatMbType());
        }
        if (StringUtils.isEmpty(wxConfigDTO.getMenuConfig())){
            wxConfigDTO.setMenuConfig(WxConstant.DEFAULT_WECHAT_MENU_CONFIG);
        }
        return wxConfigDTO;
    }

    @Override
    public void del(int id) {
        wxConfigMapper.deleteByPrimaryKey(id);
    }

    @Override
    public WxConfig getWxConfigByWxOriginId(String wxOriginId) {
        WxConfig wxConfig = new WxConfig();
        wxConfig.setWxOriginId(wxOriginId);
        return wxConfigMapper.selectOne(wxConfig);
    }

    @Override
    public List<WxConfigDTO> getGmWxConfigList(WxConfigDTO configDTO) {
        return wxConfigMapper.getGmWxConfigList(configDTO);
    }

    @Override
    public void deleteGmWxConfig(Integer id) {
        wxConfigMapper.deleteByPrimaryKey(id);
    }
}

